<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Short description for file
 *
 * PHP version 5.3.+
 *
 * @category   
 * @package    
 * @author     Marius Schmidt <marius.schmidt@gridground.de>
 * @copyright  2010-2012 Gridground UG (haftungsbeschränkt)
 * @license    http://www.gridground.de/license/brickscout.txt
 * @version    GIT: $Id$
 * @link       
 * @see        
 * @since      File available since 2012-02-17
 */

/**
 * Short description for class
 *
 * @category   
 * @package    
 * @author     Marius Schmidt <marius.schmidt@gridground.de>
 * @copyright  2010-2012 Gridground UG (haftungsbeschränkt)
 * @license    http://www.gridground.de/license/brickscout.txt
 * @version    Release: @package_version@
 * @link       
 * @see        
 * @since      Class available since Release 
 */
class Economic_GetDataArrayCommand implements Economic_RemoteCommand {
    
    private $_mediator;
    private $_entities;
    
    public function __construct(Economic_RemoteCommandMediator $mediator, array $entities) {
        $this->validateArrayTypes($entities);
        $this->_mediator = $mediator;
        $this->_entities = $entities;
    }
    
    public function validateArrayTypes(array $entities) {
        if (empty($$this->_entities)) {
            $msg = 'Can not call because entityHandleArray is empty';
            throw new Exception($msg);
        }
        $firstElementClass = get_class($entity[0]);
        if (!$firstElementClass instanceof Economic_Entity) {
            $msg = 'Can not call because all entityHandles must implement Economic_Entity';
            throw new Exception($msg);
        }
        foreach ($$this->_entities as $entity) {
            if ($firstElementClass !== get_class($entity)) {
                $msg = 'Can not call because all entityHandles must be the same class';
                throw new Exception($msg);
            }
        }
    }
    
    public function execute() {
        $result = $this->_mediator->soapCall($this->getFunctionName(), $this->getParams());
        $key = $this->_entity->getEconomicObjectClassName() . '_GetDataArrayResult';
        return $result->$key;
    }
    
    public function getFunctionName() {
        return $this->_entities[0]->getEconomicObjectClassName() . '_GetDataArray';
    }
    
    public function getParams() {
        return array(
            'entityHandles' => $this->_entities,
        );
    }
    
    
}
